package backboneCode;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReaderXMLClass {
	public void readerXML(int nodeOrder)
	{
		
		String resultReadXML[][];
		resultReadXML= new String[3][3];
		
		   try {
				String filepath = "E:\\HelloWorld.xml";
				DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
				Document doc = docBuilder.parse(filepath);
		 
				Node ObjectDetail = doc.getFirstChild();
				Node object = doc.getElementsByTagName("Object").item(nodeOrder);
		 
				NamedNodeMap attr = object.getAttributes();
				Node nodeAttr = attr.getNamedItem("id");
				nodeAttr.setTextContent("n+1");
		 

				NodeList list = object.getChildNodes();
		 
				for (int i = 0; i < list.getLength(); i++) {
		 
		           Node node = list.item(i);
		 
				   if ("Obj_Id".equals(node.getNodeName())){
					   resultReadXML[0][nodeOrder] = node.getTextContent();
				   }
					   
				   if ("Obj_Name".equals(node.getNodeName())) {
					   resultReadXML[1][nodeOrder] = node.getTextContent();
				   }

				   if ("Obj_Data".equals(node.getNodeName())) {
					   resultReadXML[2][nodeOrder] = node.getTextContent();
				   }

		 
				}
		 

				TransformerFactory transformerFactory = TransformerFactory.newInstance();
				Transformer transformer = transformerFactory.newTransformer();
				DOMSource source = new DOMSource(doc);
				StreamResult result = new StreamResult(new File(filepath));
				transformer.transform(source, result);
		 
				System.out.println("Done");
				System.out.println(resultReadXML[0][nodeOrder]);
				System.out.println(resultReadXML[1][nodeOrder]);
				System.out.println(resultReadXML[2][nodeOrder]);
		 
			   } catch (ParserConfigurationException pce) {
				pce.printStackTrace();
			   } catch (TransformerException tfe) {
				tfe.printStackTrace();
			   } catch (IOException ioe) {
				ioe.printStackTrace();
			   } catch (SAXException sae) {
				sae.printStackTrace();
			   }
	}
}
